package com.dorje.sys.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;
import com.dorje.sys.entity.Menu;
import com.dorje.sys.service.MenuService;

import cc.ysblog.util.TreeUtil;

@Controller
@RequestMapping("/menu/")
public class MenuController {
	
	@Autowired
	MenuService menuService;
	
	@RequestMapping("index")
	public String index(){
		return "sys/menu";
	}
	
	@RequestMapping("list")
	public void list(HttpServletRequest request,HttpServletResponse response) throws IOException{
		List<Menu> menus =  menuService.findAll(null);		
		response.setContentType("text/html;charset=utf-8");
		String str = JSON.toJSONStringWithDateFormat(TreeUtil.getMenuTree(menus),"yyyy-MM-dd HH:mm:ss.SSS");
		response.getWriter().write(str);
	}
	
	@RequestMapping("add")
	public String add(Model model){
		Menu menu = new Menu();
		model.addAttribute("menu", menu);
		return "/sys/menu/info";
	}
	
	@RequestMapping("edit")
	public String edit(@RequestParam("id") Integer id,Model model){
		model.addAttribute("menu", menuService.findInfoById(id));
		return "/sys/menu/info";
	}
	
	@RequestMapping("save")
	public void save(@ModelAttribute Menu menu, HttpServletRequest request,HttpServletResponse response) throws IOException{
		String msg = null;
		if(menu.getName().length() < 2){
			msg = "菜单名称";
		}else{
			response.getWriter().write(menu.toString());
		}
		
	}
	
}
